package com.yingxin.yxerp.module.print.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yx.framework.render.core.RenderApi;
import com.yingxin.yxerp.module.activiti.api.ProcessQueryService;
import com.yingxin.yxerp.module.print.entity.PrintApply;
import com.yingxin.yxerp.module.print.mapper.PrintApplyMapper;
import com.yingxin.yxerp.module.print.service.IPrintApplyService;
import com.yingxin.yxerp.module.print.vo.req.PrintApplyCreateVo;
import com.yingxin.yxerp.module.print.vo.req.PrintApplyQueryVo;
import com.yingxin.yxerp.module.print.vo.resp.PrintApplyRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 打印申请 前端控制器
 * @author fanchao
 * @since 2022-03-21
 */

@RestController
@RequestMapping("/print/apply")
public class PrintApplyController {

    @Autowired
    private IPrintApplyService printApplyService;

    @SuppressWarnings("all")
    @Autowired
    private PrintApplyMapper printApplyMapper;

    @Autowired
    private ProcessQueryService processQueryService;

    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<PrintApplyRespVo> page(@RequestBody PrintApplyQueryVo queryVo) {
        IPage<PrintApplyRespVo> page = printApplyMapper.pageVo(PageHelper.in(queryVo), queryVo);
        processQueryService.collectTaskId(page.getRecords(), PrintApplyRespVo::getId, BusinessType.DYSQ);
        return PageHelper.out(page);
    }

    @RenderApi
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public PrintApplyRespVo get(@PathVariable("id") String id) {
        PrintApplyRespVo apply = printApplyMapper.getVo(id);
        apply.setActivitiInstance(processQueryService.getProcessByBusinessKey(apply.getId()));
        return apply;
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid PrintApplyCreateVo createVo) {
        PrintApply apply = new PrintApply();
        BeanUtil.copyProperties(createVo, apply);
        printApplyService.create(apply);
        return apply.getId();
    }

}